package com.smartwebee.android.blespp.hospital;

import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.google.gson.Gson;
import com.smartwebee.android.blespp.R;
import com.smartwebee.android.blespp.databinding.ActivityMyPlansBinding;
import com.smartwebee.android.blespp.hospital.adapter.PlanExpandableAdapter;
import com.smartwebee.android.blespp.http.HttpRequest;
import com.smartwebee.android.blespp.http.RequestInterface;
import com.smartwebee.android.blespp.http.VolleyReqQueue;
import com.smartwebee.android.blespp.resp.BaseResp;
import com.smartwebee.android.blespp.resp.GetPlanResp;
import com.smartwebee.android.blespp.utils.UIUtils;
import com.smartwebee.android.blespp.utils.Utils;

import java.util.HashMap;
import java.util.Map;

public class MyPlansActivity extends BaseActivity implements RequestInterface {

    ActivityMyPlansBinding binding;
    private PlanExpandableAdapter expandableAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_my_plans);
        TAG = "MyPlansActivity";
        initView();
    }

    private void initView() {
        binding.btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        expandableAdapter = new PlanExpandableAdapter(this);
        binding.listviewPlan.setAdapter(expandableAdapter);
        expand();
        binding.listviewPlan.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true;
            }
        });
        initData();
    }

    private void expand() {
        for (int i = 0; i < expandableAdapter.getGroupCount(); i++) {
            binding.listviewPlan.expandGroup(i);
        }
    }

    private void initData() {
        requestQueue = VolleyReqQueue.getInstance(this).getRequestQueue();
        httpGetPlan();
    }

    private void httpGetPlan() {
        showLoading();
        HttpRequest httpRequest = new HttpRequest(Request.Method.POST, Utils.getPlan,
                this) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("sn", Utils.registerResp.getData().getSn());
                return map;
            }
        };
        requestQueue.add(httpRequest);
    }

    private void deletePlan() {
        expandableAdapter.clearItems();
        Utils.childItem = expandableAdapter.getChildItem();
    }

    @Override
    public void onSuccess(String id, String resp) {
        dismissLoading();
        switch (id) {
            case Utils.getPlan:
                GetPlanResp getPlanResp = new Gson().fromJson(resp, GetPlanResp.class);
                if (Utils.parseData(getPlanResp)) {
                    deletePlan();
                    for (int i = 0; i < getPlanResp.getData().size(); i ++) {
                        String[] pids = getPlanResp.getData().get(i).getPid().split(",");
                        for (int j = 0; j < pids.length; j ++) {
                            expandableAdapter.addChildItem(Integer.valueOf(getPlanResp.getData().get(i).getJid()) - 1,
                                    Utils.getModeNameById(pids[j]));
                            expand();
                            Utils.childItem = expandableAdapter.getChildItem();
                        }
                    }
                } else {
                    UIUtils.showTips(this, getPlanResp.getMsg());
                }
                break;
        }
    }

    @Override
    public void onError(String error) {
        dismissLoading();
        UIUtils.showTips(this, error);
    }
}
